<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="手机号">
        <el-input v-model="userInfo.mobile" placeholder="手机号" />
      </el-form-item>
      <el-form-item label="用户类型">
        <el-select v-model="userInfo.userType" placeholder="请选择" clearable>
          <el-option label="投资人" value="1" />
          <el-option label="借款人" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户状态">
        <el-select v-model="userInfo.status" placeholder="请选择" clearable>
          <el-option label="正常" value="1" />
          <el-option label="锁定" value="0" />
        </el-select>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="fetchData()">
        查询
      </el-button>
      <el-button type="default" @click="resetData()"> 清空 </el-button>
    </el-form>
    <el-table :data="list" border stripe>
      <el-table-column label="#" width="50">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="用户类型" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.userType === 1" type="success" size="mini">
            投资人
          </el-tag>
          <el-tag v-if="scope.row.userType === 2" type="warning" size="mini">
            借款人
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="mobile" label="手机号" />
      <el-table-column prop="name" label="用户姓名" />
      <el-table-column prop="idCard" label="身份证号" />
      <el-table-column prop="integral" label="用户积分" />
      <el-table-column prop="createTime" label="注册时间" />
      <el-table-column label="绑定状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.bindStatus === 1" type="success" size="mini">
            已绑定
          </el-tag>
          <el-tag v-if="scope.row.bindStatus === 0" type="warning" size="mini">
            未绑定
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="用户状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success" size="mini">
            正常
          </el-tag>
          <el-tag v-if="scope.row.status === 0" type="info" size="mini">
            锁定
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="170" align="center">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === 1"
            size="mini"
            type="danger"
            @click="lockAndUnlock(scope.row.id, scope.row.status)"
          >
            锁定
          </el-button>
          <el-button
            v-if="scope.row.status === 0"
            size="mini"
            type="success"
            @click="lockAndUnlock(scope.row.id, scope.row.status)"
          >
            解锁
          </el-button>
          <el-button size="mini" type="primary" @click="loginLog(scope.row.id)">
            登录日志
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[10, 15, 20]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog title="登录日志" :visible.sync="dialogTableVisible" center>
      <el-table :data="loginRecordList" border stripe>
        <el-table-column property="ip" label="IP" />
        <el-table-column property="updateTime" label="登陆时间" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import userInfoApi from "@/api/core/user-info";
import userLoginRecordApi from "@/api/core/user-login-record";
export default {
  data() {
    return {
      list: null,
      total: 0,
      userInfo: {},
      page: 1,
      limit: 10,
      loginRecordList: [],
      dialogTableVisible: false,
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      userInfoApi
        .list(this.page, this.limit, this.userInfo)
        .then((response) => {
          this.list = response.data.list.records;
          this.total = response.data.list.total;
        });
    },
    handleSizeChange(response) {
      this.limit = response;
      this.fetchData();
    },
    handleCurrentChange(response) {
      this.page = response;
      this.fetchData();
    },
    resetData() {
      this.userInfo = {};
      this.fetchData();
    },
    lockAndUnlock(id, status) {
      console.log(id, status);
      status = status == 0 ? 1 : 0;
      console.log(id, status);
      userInfoApi.lockAndUnlock(id, status).then((response) => {
        this.$message.success(response.msg);
        this.fetchData();
      });
    },
    loginLog(id) {
      this.dialogTableVisible = true;
      userLoginRecordApi.list(id).then((response) => {
        this.loginRecordList = response.data.userLoginRecords;
      });
    },
  },
};
</script>